package com.jcvision.mms.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jcvision.mms.dao.AuditOpinionDao;
import com.jcvision.mms.dao.FrameDao;
import com.jcvision.mms.dao.MmsDao;
import com.jcvision.mms.model.AuditOpinion;
import com.jcvision.mms.model.Frame;
import com.jcvision.mms.model.FrameExample;
import com.jcvision.mms.model.Mms;
import com.jcvision.mms.model.MmsExample;
import com.jcvision.mms.model.MmsExample.Criteria;
import com.jcvision.mms.util.Constant;
import com.jcvision.mms.util.ErrorMsg;
import com.jcvision.mms.util.PageBean;

@Controller
@RequestMapping("/module/audits")
public class AuditsController extends BaseController {

	@SuppressWarnings("unchecked")
	@RequestMapping("/list.do")
	public String list(Mms mms, Integer pageNumber, ModelMap map){
		try{
			MmsDao mmsDao = new MmsDao();
			MmsExample mmsExample = new MmsExample();
			Criteria criteria = mmsExample.createCriteria();
			criteria.andStateEqualTo(Constant.STATE_WAIT);
			if(mms != null && mms.getName() != null){
				mms.setName(new String(mms.getName().getBytes("iso-8859-1"), "UTF-8"));
				criteria.andNameLike("%" + mms.getName() + "%");
			}
			PageBean pageBean = new PageBean();
			if(pageNumber != null){
				pageBean.setCurrentPage(pageNumber);
			}
			List<Mms> rows = (List<Mms>) mmsDao.select(mmsExample, pageBean);
			map.put("pageBean", pageBean);
			map.put("rows", rows);
			map.put("stateMap", Constant.stateMap);
			return "forward:/module/audits/list.jsp";
		}catch(Exception e){
			e.printStackTrace();
			map.put(ERROR_KEY, ErrorMsg.DAO_EXCEPTION);
			return ERROR_PAGE;
		}
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/toAudits.do")
	public String toAudits(Long id, String fromUrl, ModelMap map){
		map.put(FROM_URL, fromUrl);
		try{
			if(id != null){
				MmsDao mmsDao = new MmsDao();
				Mms mms = (Mms) mmsDao.select(id);
				if(mms != null){
					FrameExample frameExample = new FrameExample();
					frameExample.createCriteria().andMmsIdEqualTo(mms.getId());
					FrameDao frameDao = new FrameDao();
					List<Frame> frames = (List<Frame>) frameDao.select(frameExample);
					
					map.put("mms", mms);
					map.put("frames", frames);
					map.put("stateMap", Constant.stateMap);
					return "forward:/module/audits/audits.jsp";
				}
			}
			map.put(ERROR_KEY, ErrorMsg.NOT_EXIST_DB);
		}catch(Exception e){
			e.printStackTrace();
			map.put(ERROR_KEY, ErrorMsg.DAO_EXCEPTION);
		}
		return ERROR_PAGE;
	}
	
	@RequestMapping("/audits.do")
	public String audits(Mms mms, AuditOpinion auditOpinion, String fromUrl, ModelMap map){
		try{
			if(mms != null && mms.getId() != null && mms.getState() != null){
				Mms record = new Mms();
				record.setId(mms.getId());
				record.setState(mms.getState());
				MmsDao mmsDao = new MmsDao();
				int count = mmsDao.updateByPrimaryKeySelective(record);
				if(auditOpinion.getOpinion() != null && !"".equals(auditOpinion.getOpinion().trim())){
					auditOpinion.setId(null);
					auditOpinion.setMmsId(mms.getId());
					AuditOpinionDao auditOpinionDao = new AuditOpinionDao();
					auditOpinionDao.insert(auditOpinion);
				}
				if(count > 0){
					return "redirect:" + fromUrl;
				}
			}
			map.put(ERROR_KEY, ErrorMsg.NOT_EXIST_DB);
		}catch(Exception e){
			e.printStackTrace();
			map.put(ERROR_KEY, ErrorMsg.DAO_EXCEPTION);
		}
		return ERROR_PAGE;
	}
	
}
